package com.example.paper1024.imply;

import com.example.paper1024.dao.UserDao;
import com.example.paper1024.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserImply implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public User queryUserByUsername(String username) {
        String sql = "SELECT * FROM user WHERE username = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username);
        } catch (Exception e) {
            return null; // 用户不存在
        }
    }

    @Override
    public boolean addUser(User user) {
        String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
        return jdbcTemplate.update(sql, user.getUsername(), user.getPassword()) > 0;
    }
}
